-- remote_attack
-- created by panyl
-- 每若干轮自动攻击

return {
    apply = function(prop, baseValue)
        return baseValue + prop[3];
    end,

    -- 每回合触发
    trigger = function(who, prop, arg)
        -- 有无敌状态
        if #PropM.fetchProps(ME.user, "invincible") > 0 then
            return false;
        end

        -- 是不是攻击的回合
        local grid = DungeonM.getGridByPos(who:getPos());
        local openRound = grid.openRound or 0;
        local step = tonumber(prop[2]);
        local round = CombatM.getRound();
        if (round - openRound) % step ~= 0 then
            return false;
        end

        -- 标明该回合不能远程攻击
        if who.dbase:query("hit_by_user") == round then
            return false;
        end

        if round <= openRound then
            return false;
        end

        local msg = string.format("%s远程攻击，hit_by_user = %s", who:query("name") or "", who.dbase:query("hit_by_user", 0));
        trace("remote_attack", msg);
        -- 初始化序列
        SkillM.initSequence(who, who, 0);
        -- 远程怪攻击动作
        SkillM.getSequence(who):remoteAttack(who, arg, PHYSIC_ATTACK);
        -- 是否闪避了
        if not FormulaM.invoke("HAPPEN_DODGE", who, arg, DungeonM.getRandSeed("REMOTE_ATTACK_DODGE")) then
            -- 是否免疫了远程
            local p = PropM.combine(arg, "parry_ranged_damage", 1);
            local rand = DungeonM.getRandSeed("parry_ranged_damage") % 1000;
            if rand > p[3] then
                SkillM.hit(who, arg, 0);
                SkillM.getSequence(who):start(who, arg, 0);
                if arg.type == OBJECT_TYPE_USER then
                    -- 法力燃烧，消耗目标魔法
                    local propId = PropM.getPropId("remote_burn_mp");
                    PropM.trigger(who, propId, 1, arg);

                    -- 清除状态
                    propId = PropM.getPropId("remote_clear_status");
                    PropM.trigger(who, propId, 1, arg);
                end
            else
                trace("remote_attack", "%s格挡。", arg:getName());
                SkillM.initSequence(arg, arg, 0);
                SkillM.getSequence(arg):start(who, arg, 0);
                SkillM.getSequence(arg):immunity(who, arg);
            end
        else
            trace("remote_attack", "%s闪避。", arg:getName());
            SkillM.initSequence(arg, arg, 0);
            SkillM.getSequence(arg):start(who, arg, 0);
            SkillM.getSequence(arg):dodge(who, arg);
            EventMgr.fire(event.USER_DODGE);
        end

        return true;
    end,

    clear = function(who, prop)
        -- 同步清除掉改变技能间隔的属性
        PropM.deletePropById(who, "special", "add_cd");
    end,

    record = function(target, prop, path)
        return false;
    end
};
